<?php

class Helpermodel extends CI_Model {

	function menu_active_controller($match, $control) {
		if ($match == $control) {
			echo "current";
		}
	}

	function menu_active_item($match, $item) {
		if ($match == $item) {
			echo "sub_show";
		}
	}

	function menu_active_showchild($match, $item) {
		if ($match == $item) {
			echo "show";
		}
	}

	function defaultimage($filename) {
		if ($filename == "") {
			return "<img src='" . base_url() . "images/no_img_default.png' />";
		} else {
			return "<img src='" . $this->config->item('media_folder_listing') . $filename . "' />";
		}
	}

	function getfilenameimage($filename) {
		if (!empty($filename)) {
			$filename = explode('/', $filename);

			$filename = end($filename);

			$filename = substr($filename, 0, -4);

			return $filename;
		} else {
			$filename = 'default';
			return $filename;
		}
	}

	function price_converttobase($amount, $unit) {
		$convert_amt = 0;

		//$country_rate = IDR_TO_USD;
		
		$country_rate = 1;
        if ($unit == 'USD') {
            $country_rate = 1;
        } else {
            $country_rate_obj = $this->getRateByUnitCurrency($unit);
            if (isset($country_rate_obj)) {
                $country_rate = $country_rate_obj;
            }
        }

		$convert_amt = $amount * $country_rate;

		//return $convert_amt;

		return number_format($convert_amt, 2, '.', ',');
	}

	function price_converttobase_2($amount, $unit) {
		$convert_amt = 0;

		$country_rate_obj = $this->getRateByUnitCurrency($unit);

		$country_rate = 1;
		if ($unit == 'USD') {
			$country_rate = 1;
		} else {
			if (isset($country_rate_obj)) {
				$country_rate = $country_rate_obj;
			}
		}

		$convert_amt = $amount * $country_rate;

		return $convert_amt;

		//return $unit . " " .number_format ($convert_amt, 2, '.',',');
	}

	function price_converttounit($amount, $unit) {
		$country_usd_amount = 0;

		$country_rate = 1;
		if ($unit == 'USD') {
			$country_rate = 1;
		} else {
			$country_rate_obj = $this->getRateByUnitCurrency($unit);
			if (isset($country_rate_obj)) {
				$country_rate = $country_rate_obj;
			}
		}

		if ($country_rate > 0) {
			$country_usd_amount = $amount / $country_rate;
		} else {
			$country_usd_amount = $amount;
		}

		return number_format($country_usd_amount, 5, '.', '');
	}

	function getRateByUnitCurrency($unit) {
		//once webservice is updated, we will take out the "rate > 0"
		$tsql = "{call sp_get_curency_rate(?)}";
		$params = array(
		array($unit, SQLSRV_PARAM_IN)
		);
		$resut = null;
		$query = $this->db->exec_query($tsql, $params);

		while ($obj = sqlsrv_fetch_object($query)) {
			$resut = $obj->Rate;
		}
		return $resut;
	}

	function price_converttocurrency($currency_name, $amount, $currency_rate) {

		$convert_amt = $amount * $currency_rate;


		return $currency_name . " " . number_format(ceil($convert_amt), 2, '.', ',');
	}

	function price_converttocurrency_2($currency_value, $amount) {
		$convert_amt = "";
		$amount = (int) $amount;

		//$currency_value = 2: USD
		//$currency_value = 1: IDR
		//$currency_rate = 9433.96226: 1USD = 9433.96226 IRD
		if ($currency_value == 2) {
			$convert_amt = $amount * 9433.96226; // sq meter
			return $convert_amt;
		} else if ($currency_value == 1) {
			return $amount;
		}
	}

	//format to display on result page
	function price_convert_per_unit($price, $unit) {

		$convert_price = "";

		if ($unit == 'psm') {
			$convert_price = $price;

			return $convert_price;

			//return number_format( $convert_area, 2 ,'.','') . ' ' . $unit;
		} else if ($unit == 'psf') {
			$convert_price = $price / 10.7639104; //sq foot
			//return $convert_price;

			return number_format($convert_price, 2, '.', '');
		}
	}

	function area_sqfttounit($area, $unit) {

		$convert_area = "";

		if ($unit == 'sqm') {
			$convert_area = $area * 0.09290304; // sq meter
			return number_format($convert_area, 0, '.', '');
		} else if ($unit == 'sqyds') {
			$convert_area = $area * 0.111111111; //sq yards
			return number_format($convert_area, 0, '.', '');
		} else {
			return $area;
		}
	}

	//format to display on result page
	function area_sqmtounit_tobase($area, $unit) {
		$convert_area = 0;

		if ($unit == 'sqm') {
			$convert_area = $area;

			//return $convert_area*1;
			if ($area != 0) {
				return number_format($convert_area, 2, '.', ',');
			} else {
				return $area;
			}
		} else if ($unit == 'sqft') {
			$convert_area = $area * SQM_TO_SQFT; //sq foot
			//return $convert_area;
			if ($area != 0) {
				return number_format($convert_area, 0, '.', ',');
			} else {
				return $area;
			}
		}
	}

	function area_sqmtounit_tobase_no_format($area, $unit) {
		$convert_area = 0;

		if ($unit == 'sqm') {
			$unit = 'sq.m.';
			$convert_area = $area;

			//return $convert_area*1;
			if ($area != 0) {
				return $convert_area;
			} else {
				return $area;
			}
		} else if ($unit == 'sqft') {
			$unit = 'sq.ft.';
			$convert_area = $area * SQM_TO_SQFT; //sq foot
			//return $convert_area;
			if ($area != 0) {
				return $convert_area;
			} else {
				return $area;
			}
		}
	}

	//format to display on result page
	function area_sqmtounit_tobase_2($area, $unit) {

		$convert_area = 0;

		if ($unit == 'sqm') {
			$convert_area = $area;

			return number_format($convert_area * 1, 2, '.', '');
		} else if ($unit == 'sqf') {
			$convert_area = $area * 10.7639104; //sq foot

			return number_format($convert_area, 2, '.', '');
		}
	}

	//format size to sqm
	function area_sqmtounit($area, $unit) {

		$convert_area = "";

		if ($unit == 'sqm') {
			$convert_area = $area;

			return number_format($convert_area, 2, '.', '');
		} else if ($unit == 'sqf') {
			$convert_area = $area * 0.09290304000008391; //sq meter
			return number_format($convert_area, 2, '.', '');
		}
	}

	//format size to sqm
	function area_converter_tool($area, $unit) {
		$convert_area = 0;

		if (!is_numeric($area)) {
			$convert_area = 0;
		} else {

			if ($unit == 'sqm') {
				$convert_area = $area;
			} else if ($unit == 'sqft') {
				$convert_area = $area * 0.09290304000008391; //sq meter
			}
		}
		return $convert_area;
	}

	//added by Chi
	function area_unittosqft($area, $unit, $unit_need_converted_to) {
		$convert_area = "";
		$unitmeasurement = "";

		$unitmeasurement_sqf = 'sqf';
		$unitmeasurement_sqm = 'sqm';

		if ($unit == 'sqm') {
			$unit = 1;
		} else {
			$unit = 4;
		}

		if ($area == "" || is_null($area)) {
			return NULL;
		} else {
			if ($unit == $unit_need_converted_to) {
				return number_format($area, 0, '.', '') . ' ' . $unitmeasurement_sqm;
			} else {
				if ($unit == 1) {
					$convert_area = $area * 10.7639104; // sq foot

					return number_format($convert_area, 0, '.', '') . ' ' . $unitmeasurement_sqf;
				} else if ($unit == 4) {
					$convert_area = $area * 0.09290304000008391; //sq meter

					return number_format($convert_area, 0, '.', '') . ' ' . $unitmeasurement_sqm;
				} else {
					return $area;
				}
			}
		}
	}

	function check_is_array($_array) {
		if (!is_array($_array) || empty($_array)) {
			return false;
		}
		$next = 0;

		foreach (array_keys($_array) as $k => $v) {
			if ($k != $v) {
				return false;
			}
			$next++;
		}

		foreach ($_array as $k => $v) {
			if (empty($v)) {
				return false;
			}
		}

		return true;
	}

	//get extention of image
	function get_extension($filename) {
		$x = explode('.', $filename);
		return '.' . end($x);
	}

	// remove directory and all subdirectories
	function recursive_rmdir($dir) {
		if (is_dir($dir)) {
			if ($open_dir = opendir($dir)) {
				while ($file = readdir($open_dir)) {
					if ($file != '.' && $file != '..') {
						recursive_rmdir($dir . '/' . $file);
					}
				}
				closedir($open_dir);
			}
			@rmdir($dir);
		} elseif (is_file($dir)) {
			@unlink($dir);
		}
	}

	function convert_priceperunit($priceperunit, $unit){
		$value = 0;
		 
		if ($unit == 'sqm') {
			$value = $priceperunit;
		} else {
			$value = $priceperunit / SQM_TO_SQFT; //sq foot
		}

		if ($value != 0) {
			return number_format($value, 0, '.', ',');
		} else {
			return $value;
		}
	}

}